package com.gaff.emp.core.modules.sys.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;

import java.text.DateFormat;
import java.time.Instant;
import java.util.Date;


@Getter
@Setter
@TableName("sys_log")
public class SysLog {

    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 操作类型 1 操作记录 2异常记录
     */
    private Integer type;

    private Integer userId;

    private String username;

    private String operation;

    private String method;

    private String params;

    private Integer consumingTime;

    private String ip;

    /**
     * 异常描述 e.getMessage
     */
    private String exDesc;

    /**
     * 异常详情信息 堆栈信息
     */
    private String exDetail;

    private Date createTime = new Date();

    @TableField(exist = false)
    private boolean isPersistence = false;

    @TableField(exist = false)
    private long startTime = Instant.now().toEpochMilli();

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder("\n=========================================\n");
        sb.append("   access_time: " + DateFormat.getDateTimeInstance().format(createTime) + "\n");
        sb.append("     access_ip: " + ip + "\n");
        sb.append("       user_id: " + (userId == null ? "NULL" : userId) + "\n");
        sb.append("      username: " + username + "\n");
        sb.append("     operation: " + operation + "\n");
        sb.append("        method: " + method + "\n");
        sb.append("        params: " + params + "\n");
        sb.append("consuming_time: " + consumingTime + "ms\n");
        sb.append("        status: " + (type == 1 ? "success" : "error") + "\n");
        if (type != 1) {
            sb.append("        exDesc: " + exDesc + "\n");
            sb.append("      exDetail: " + exDetail + "\n");
        }
        sb.append("=========================================\n");
        return sb.toString();
    }
}
